// pages/store/store.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select:'store',
    loadmore: 'block',
    nodata: 'none',
    nomore: 'none',
    page: 1,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ select: options.select });
    if(options.select=="store"){
      this.Find_Store();
    }else{
      this.Find_History();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var select=this.data.select;
    if (this.data.page > 1) {  //必须加上，否则上拉和下拉会同时触发
      if (select == "store") {
        this.Find_Store();
      } else {
        this.Find_History();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  Find_Store: function () {
    var that = this;
    var data = {};
    data.act = "Find_Store";
    data.page = that.data.page;
   
    app.Ajax(data, function (json) {
      var pagecount = Math.floor(Math.abs(json.count - 1) / json.pagesize) + 1;
      if (data.page > pagecount) { return; }
      that.setData({ list: that.data.list.concat(json.data), page: data.page + 1 });
      if (that.data.list.length == 0) {
        that.setData({ loadmore: 'none', nodata: 'block', nomore: 'none' });
      }
      else if (pagecount == 1) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'none' });
      }
      else if (data.page < pagecount) {
        that.setData({ loadmore: 'block', nodata: 'none', nomore: 'none' });
      } else if (data.page == pagecount) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'block' });
      }
      wx.stopPullDownRefresh();
    });
  },
  Find_History: function () {
    var that = this;
    var data = {};
    data.act = "Find_History";
    data.page = that.data.page;
    app.Ajax(data, function (json) {
      var pagecount = Math.floor(Math.abs(json.count - 1) / json.pagesize) + 1;
      if (data.page > pagecount) { return; }
      that.setData({ list: that.data.list.concat(json.data), page: data.page + 1 });
      if (that.data.list.length == 0) {
        that.setData({ loadmore: 'none', nodata: 'block', nomore: 'none' });
      }
      else if (pagecount == 1) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'none' });
      }
      else if (data.page < pagecount) {
        that.setData({ loadmore: 'block', nodata: 'none', nomore: 'none' });
      } else if (data.page == pagecount) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'block' });
      }
      wx.stopPullDownRefresh();
    });
  },
  openProductInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/productinfo/productinfo?id=' + id
    })
  },
  changetab: function (e) {
    var select = e.currentTarget.dataset.select;
    this.setData({ list: [], page: 1, loadmore: 'block', nodata: 'none', nomore: 'none', select: select});
    if (select == "store") {
      this.Find_Store();
    } else {
      this.Find_History();
    }
  }
})